package com.itheima.zuoye2.zy4;

import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.util.*;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        TreeMap<String, Integer> students = new TreeMap<>();

        while (true) {
            System.out.println("请输入学生信息（格式：姓名,年龄），例如：张三,18：,按ok键结束");
            String input = scanner.nextLine().trim();
            if ("ok".equalsIgnoreCase(input)) break;

            // 按逗号分割
            String[] parts = input.split(",");
            if (parts.length != 2) {
                System.out.println("格式错误！请按“姓名,年龄”输入。");
                return;
            }

            String name = parts[0].trim();
            try {
                int age = Integer.parseInt(parts[1].trim());
                students.put(name, age);
                System.out.println("添加成功！当前学生列表：" + students);
            } catch (NumberFormatException e) {
                System.out.println("年龄必须是整数！");
            }
        }
        System.out.println("所有学生信息：" + students);
        FileUtil.writeUtf8Lines(students.entrySet(), "D:\\游戏\\ikun合集\\新建文件夹\\student.txt");
    }


        }
